// var VSHADER_SOURCE = 'attribute vec4 a_Position;\n' +
// 'uniform float u_CosB, u_SinB;\n' +
// 'void main() {\n' +
// '  gl_Position.x = a_Position.x * u_CosB ;\n' +
// '  gl_Position.y = a_Position.x * u_SinB ;\n' +
// '  gl_Position.z = a_Position.z;\n' +
// '  gl_Position.w = 1.0;\n' +
// '}\n';
// sin(x + y) = sin x * cos y + cos x * sin y;
var VSHADER_SOURCE = `
    attribute vec4 a_Position;
    uniform vec4 u_Translation;
    uniform float u_SinB,u_CosB;
    void main(){
      gl_Position.x = a_Position.x * u_CosB - a_Position.y * u_SinB + u_Translation.x;
      gl_Position.y = a_Position.y * u_CosB + a_Position.x * u_SinB + u_Translation.y;
      gl_Position.z = 0.0;
      gl_Position.w = 1.0;
      // gl_PointSize = 10.0;
    }
  `
var FSHADER_SOURCE = `
    precision mediump float;
    uniform vec4 u_Color;
    void main() {
      // gl_FragColor = u_Color;
      gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
  `;
var canvas = document.getElementById('example');
var gl = getWebGLContext(canvas);

function clear() {
  // Specify the color for clearing <canvas>
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // Clear <canvas>
  gl.clear(gl.COLOR_BUFFER_BIT);
}

// The translation distance for x, y, and z direction
var Tx = 0.5,
  Ty = 0.5,
  Tz = 0.0;

function initVertexBuffers(gl) {
  var vertices = new Float32Array([
    0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5
  ]);
  var n = 3; // The number of vertices
  var vertexBuffer = gl.createBuffer();
  var a_Position = gl.getAttribLocation(gl.program, 'a_Position');

  if (!vertexBuffer) {
    console.log('Failed to create the buffer object');
    return -1;
  }

  if (a_Position < 0) {
    console.log('Failed to get the storage location of a_Position');
    return -1;
  }
  console.log("vertexBuffer", vertexBuffer)
  // Bind the buffer object to target
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);

  // Write date into the buffer object
  gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

  // Assign the buffer object to a_Position variable
  gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);

  // Enable the assignment to a_Position variable
  gl.enableVertexAttribArray(a_Position);

  return n;
}

var ANGLE = 98.0;
// // Pass the data required to rotate the shape to the vertex shader
var radian = Math.PI * ANGLE / 180.0; // Convert to radians
var u_CosB = Math.cos(radian);
var u_SinB = Math.sin(radian);
console.log("u_SinB",u_SinB);
console.log("u_CosB",u_CosB);
function init() {
  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log('Failed to intialize shaders.');
    return;
  }

  if (initVertexBuffers(gl) < 0) {
    console.log('Failed to set the positions of the vertices');
    return;
  }

  // Pass the translation distance to the vertex shader
  var u_Translation = gl.getUniformLocation(gl.program, 'u_Translation');
  if (!u_Translation) {
    console.log('Failed to get the storage location of u_Translation');
    return;
  }

  var ucosb = gl.getUniformLocation(gl.program, 'u_CosB');
  var usinb = gl.getUniformLocation(gl.program, 'u_SinB');

  gl.uniform1f(ucosb, u_CosB);
  gl.uniform1f(usinb, u_SinB);
  clear();
  gl.uniform4f(u_Translation, Tx, Ty, Tz, 0.0);
  // Draw three points
  gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);
}

function main() {
  console.log("main")
  console.log(gl)
  init();
}